package com.zznj.xzjc.activity.person;

import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialogJBSureCancel;
import com.vondear.rxui.view.dialog.RxDialogSex;
import com.zznj.xzjc.R;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.IdCardCheck;
import com.zznj.xzjc.utils.StringUtils;
import com.zznj.xzjc.utils.TimeUtils;
import com.zznj.xzjc.view.ImageViewPlus;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.FormBody;


/**
 * Created by JY on 2017/11/15.
 */

public class PersonInfoActivity extends BaseActivity {
    @BindView(R.id.headPic)
    ImageViewPlus headPic;
    @BindView(R.id.person_tuxian_linear)
    View person_tuxian_linear;

    @BindView(R.id.userName)
    TextView userName;
    @BindView(R.id.orgName)
    TextView orgName;
    @BindView(R.id.telephone)
    TextView telephone;
    @BindView(R.id.idCard)
    TextView idCard;
    @BindView(R.id.email)
    TextView email;
    @BindView(R.id.address)
    TextView address;


    //修改用户名、性别、手机

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_personinfo_layout;
    }

    private LoginBean user;
    @Override
    public void initView() {
        setTitle("个人资料");
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        userName.setText(user.getUserName());
        orgName.setText(user.getOrgName());
        telephone.setText(user.getTelephone());
        idCard.setText(user.getIdCard());
        email.setText(user.getEmail());
        address.setText(user.getAddress());
    }

    @Override
    public void initData() {
        getUserInfo();
    }

    private void getUserInfo(){
        SubscriberOnNextListener<BaseBean<LoginBean>> onNextListener = baseBean -> {
            if (baseBean.isSuccess()) {
                user = baseBean.getResult();
                SharedHelper.saveKeyValueForString(mContext,"Member",new Gson().toJson(user));
                insertData();
            }
        };
        HttpMethods.getInstance().getUser(new ProgressSubscriber<>(onNextListener, mContext, true));
    }

    private void insertData(){
        userName.setText(user.getUserName());
        orgName.setText(user.getOrgName());
        telephone.setText(user.getTelephone());
        idCard.setText(user.getIdCard());
        email.setText(user.getEmail());
        address.setText(user.getAddress());

    }

    RxDialogJBSureCancel rxDialogEditSureCancel;
    private int type; // 0--名称；1--手机号；2--身份证；3--邮箱；4--地址
    private String content;
    private void initRxDialog() {
        rxDialogEditSureCancel = new RxDialogJBSureCancel(this);
        if(type == 0){
            rxDialogEditSureCancel.getTitleView().setText("用户名");
            rxDialogEditSureCancel.getEditText().setHint("用户名");
            rxDialogEditSureCancel.getEditText().setText(StringUtils.toString(user.getUserName()));
        }else if(type == 1){
            rxDialogEditSureCancel.getTitleView().setText("手机号");
            rxDialogEditSureCancel.getEditText().setHint("手机号");
            rxDialogEditSureCancel.getEditText().setText(StringUtils.toString(user.getTelephone()));
        }else if(type == 2){
            rxDialogEditSureCancel.getTitleView().setText("身份证");
            rxDialogEditSureCancel.getEditText().setHint("身份证");
            rxDialogEditSureCancel.getEditText().setText(StringUtils.toString(user.getIdCard()));
        }else if(type == 3){
            rxDialogEditSureCancel.getTitleView().setText("邮箱");
            rxDialogEditSureCancel.getEditText().setHint("邮箱");
            rxDialogEditSureCancel.getEditText().setText(StringUtils.toString(user.getEmail()));
        }else if(type == 4){
            rxDialogEditSureCancel.getTitleView().setText("地址");
            rxDialogEditSureCancel.getEditText().setHint("地址");
            rxDialogEditSureCancel.getEditText().setText(StringUtils.toString(user.getAddress()));
        }
        rxDialogEditSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
            }
        });
        rxDialogEditSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogEditSureCancel.dismiss();
                content = rxDialogEditSureCancel.getEditText().getText().toString();
                if(type == 0){
                    if (getString(rxDialogEditSureCancel.getEditText()).isEmpty()) {
                        RxToast.normal("请输入用户名");
                        return;
                    }
                    updateUser();
                    return;
                }
                if(type == 1){
                    if (getString(rxDialogEditSureCancel.getEditText()).isEmpty()) {
                        RxToast.normal("请输入手机号");
                        return;
                    }
                    if(!StringUtils.validateMobilePhone(getString(rxDialogEditSureCancel.getEditText()))){
                        RxToast.normal("手机号格式不正确！");
                        return;
                    }
                    updateUser();
                    return;
                }
                if(type == 2){
                    if (getString(rxDialogEditSureCancel.getEditText()).isEmpty()) {
                        RxToast.normal("请输入身份证号");
                        return;
                    }
                    if (!IdCardCheck.checkIdCardNum(getString(rxDialogEditSureCancel.getEditText()))) {
                        RxToast.normal("身份证格式不正确");
                        return;
                    }
                    updateUser();
                    return;
                }
                if(type == 3){
                    if (getString(rxDialogEditSureCancel.getEditText()).isEmpty()) {
                        RxToast.normal("请输入邮箱");
                        return;
                    }
                    updateUser();
                    return;
                }
                if(type == 4){
                    if (getString(rxDialogEditSureCancel.getEditText()).isEmpty()) {
                        RxToast.normal("请输入地址信息");
                        return;
                    }
                    updateUser();
                    return;
                }
            }
        });
        rxDialogEditSureCancel.show();
    }


    public void updateUser() {
        SubscriberOnNextListener<BaseBean<LoginBean>> onNextListener = baseBean -> {
            RxToast.normal(baseBean.getMessage());
            if(type == 0){
                userName.setText(content);
                user.setUserName(content);
            }else if(type == 1){
                telephone.setText(content);
                user.setTelephone(content);
            }else if(type == 2){
                idCard.setText(content);
                user.setIdCard(content);
            }else if(type == 3){
                email.setText(content);
                user.setEmail(content);
            }else if(type == 4){
                address.setText(content);
                user.setAddress(content);
            }
            SharedHelper.saveKeyValueForString(mContext,"Member",new Gson().toJson(user));
        };
        FormBody.Builder builder = new FormBody.Builder();
        if(type == 0){
            builder.add("userName",content);
        }else if(type == 1){
            builder.add("telephone",content);
        }else if(type == 2){
            builder.add("idCard",content);
        }else if(type == 3){
            builder.add("email",content);
        }else if(type == 4){
            builder.add("address",content);
        }
        HttpMethods.getInstance().updateUser(new ProgressSubscriber<>(onNextListener, mContext, true),builder.build());
    }

    @OnClick({R.id.userName, R.id.telephone, R.id.idCard, R.id.email, R.id.address})
    public void onViewClicked(View view) {
        if(view.getId() == R.id.userName){
            type = 0;
            initRxDialog();
        }else if(view.getId() == R.id.telephone){
            type = 1;
            initRxDialog();
        }else if(view.getId() == R.id.idCard){
            type = 2;
            initRxDialog();
        }else if(view.getId() == R.id.email){
            type = 3;
            initRxDialog();
        }else if(view.getId() == R.id.address){
            type = 4;
            initRxDialog();
        }
    }
}
